<?php
// +----------------------------------------------------------------------
// | Author: wyd 2025/10/19
// | DESC: 
// +----------------------------------------------------------------------

declare (strict_types=1);


namespace My\core\frame;

use My\core\annotations\ClassAnnotation;

#[ClassAnnotation]
class RouterCollector
{
    public $routes = [];

    /**
     * @param string $method
     * @param string $uri
     * @param \Closure $handler
     * @return void
     */
    public function addRouter(string $method, string $uri, \Closure $handler)
    {
        $this->routes[] = [
            'method' => $method,
            'uri' => $uri,
            'handler' => $handler
        ];
    }


    /**
     * @return \FastRoute\Dispatcher
     */
    public function getDispatcher()
    {
        return \FastRoute\simpleDispatcher(function (\FastRoute\RouteCollector $r) {
            foreach ($this->routes as $route){
                $r->addRoute($route['method'], $route['uri'], $route['handler']);
            }
        });
    }

}